package com.hillky.springboot.controller;

import com.hillky.springboot.common.Result;
import com.hillky.springboot.controller.request.CategoryPageRequest;
import com.hillky.springboot.entity.Category;
import com.hillky.springboot.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/category")
@CrossOrigin
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    @GetMapping("/options")
    public Result getOptions(){
       List<Category> res= categoryService.getOptions();
       return Result.success(res);
    }


    @PostMapping("/save")
    public Result save(@RequestBody Category category){
        categoryService.save(category);
        return Result.success();
    }

    @PutMapping("/")
    public Result update(@RequestBody Category category){
        categoryService.update(category);
        return Result.success();
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id){
        categoryService.deleteById(id);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id){
        Category res = categoryService.getById(id);
        return Result.success(res);
    }

    @GetMapping("/list")
    public Result list(){
        List<Category> res = categoryService.list();
        return Result.success(res);
    }

    @GetMapping("/page")
    public Result page(CategoryPageRequest categoryPageRequest) {
        return Result.success(categoryService.page(categoryPageRequest));
    }
}
